Skip to content

[ty] Preserve literal-string origin in comparison narrowing - #27582

Open
carljm wants to merge 1 commit into
mainfrom
cjm/literalstring-set-theoretic-narrowing
Open

[ty] Preserve literal-string origin in comparison narrowing#27582
carljm wants to merge 1 commit into
mainfrom
cjm/literalstring-set-theoretic-narrowing

Conversation

@carljm

@carljm carljm commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR implements a consistent view of the semantics of LiteralString and string Literal types. The key observation is that if Literal["hello"] <: LiteralString <: str holds, and LiteralString is not equivalent to str, that necessarily implies that Literal["hello"] does not include all runtime string objects with the value "hello" -- only those that are known to have literal origin (that is, those that also inhabit LiteralString).

Similar to NewType and generic specializations, this can be described in terms of typed values having both a runtime object and possibly static-only "tags", which don't exist at runtime but still participate in determining which types they inhabit. So a runtime string object with value "hello" may be a typed value that carries the "literal origin" tag (in this case it inhabits LiteralString and Literal["hello"]), or may not carry that tag (in which case it inhabits neither LiteralString nor Literal["hello"].)

This PR just carries this understanding consistently through our narrowing support.

  • Preserve existing string-origin constraints when runtime equality, identity, membership, or value-pattern comparisons can succeed.
  • Treat an excluded string literal as an impossible runtime value only when the candidate already proves LiteralString origin.
  • Restore definitive equality and inequality results for known literal-origin strings with excluded values.

Closes astral-sh/ty#4214.

Test plan

  • Added mdtests cover str & ~LiteralString, str & ~Literal["hello"], and ~Literal["hello"] across comparison truthiness and equality/identity narrowing, including reversed operands, inequality branches, optional unions, and strict equality semantics.
  • Added mdtests cover membership and match value patterns for both strings without literal origin and known literal-origin exclusions.

@astral-sh-bot astral-sh-bot Bot added the ty Multi-file analysis & type inference label Aug 7, 2026
@astral-sh-bot

astral-sh-bot Bot commented Aug 7, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 96.96%. The percentage of expected errors that received a diagnostic held steady at 92.96%. The number of fully passing files held steady at 106/133.

@astral-sh-bot

astral-sh-bot Bot commented Aug 7, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Aug 7, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

Lint rule Added Removed Changed
unsound-return-statement 0 0 1
Total 0 0 1

Flaky changes detected. This PR summary excludes flaky changes; see the HTML report for details.

Raw diff:

meson (https://github.com/mesonbuild/meson)
- mesonbuild/compilers/mixins/visualstudio.py:392:16 warning[unsound-return-statement] Unsound return statement: `list[str] | list[str | Divergent]` is not a subtype of `list[str]`
+ mesonbuild/compilers/mixins/visualstudio.py:392:16 warning[unsound-return-statement] Unsound return statement: `list[str] | list[Divergent]` is not a subtype of `list[str]`

Full report with detailed diff (timing results)

@carljm
carljm marked this pull request as ready for review August 7, 2026 20:10
@carljm
carljm requested a review from a team as a code owner August 7, 2026 20:10
@astral-sh-bot
astral-sh-bot Bot requested a review from charliermarsh August 7, 2026 20:10
@carljm
carljm requested review from AlexWaygood and removed request for charliermarsh August 7, 2026 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

clarify semantics of LiteralString, string Literal, narrowing, and set-theoretic types

1 participant